using UnityEngine;

public class CameraZone : MonoBehaviour
{
	private int fingerId = -1;

	public bool isMobile;

	public bool compensate;

	private bool pressedFromButton;

	public bool pointerDown;

	private Vector2 mouseDelta = Vector2.zero;

	private Vector2 prevTouchPos = Vector2.zero;

	private Vector2 compensationDelta = Vector2.zero;

	private Vector2 currentPointerPosition = Vector2.zero;

	private int stationaryCount;

	[HideInInspector]
	public bool shootfromButton;

	private bool shoot3Dtouch;

	private void Start()
	{
		isMobile = Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer;
		compensate = true;
	}

	public void PressFromButton(bool isDown)
	{
		if (isDown)
		{
			if (!pointerDown)
			{
				OnPress(true);
				pressedFromButton = true;
			}
		}
		else if (pressedFromButton)
		{
			OnPress(false);
			pressedFromButton = false;
		}
	}

	public void OnPress(bool isDown)
	{
		if (isDown)
		{
			SetFingerId();
			pointerDown = true;
			if (TPSCamera.instance != null)
			{
				TPSCamera.instance.OnStartRotation();
			}
			mouseDelta = Vector2.zero;
			prevTouchPos = Vector2.zero;
			return;
		}
		pressedFromButton = false;
		pointerDown = false;
		ReturnFingerId();
		if (TPSCamera.instance != null)
		{
			TPSCamera.instance.OnEndRotation();
		}
		mouseDelta = Vector2.zero;
		prevTouchPos = Vector2.zero;
	}

	public void OnDrag(Vector2 delta)
	{
	}

	public Vector2 FixedTouchDelta(Touch aTouch)
	{
		float num = Time.deltaTime / aTouch.deltaTime;
		if (num == 0f || float.IsNaN(num) || float.IsInfinity(num))
		{
			num = 1f;
		}
		return aTouch.deltaPosition * num;
	}

	private void Shoot3Dtouch()
	{
		if (!UICamera.touchPressureSupported || settings.shoot3DtouchDisabled || shootfromButton)
		{
			return;
		}
		if (PlayerBehavior.MyPlayer.currentCar == null)
		{
			if (shoot3Dtouch)
			{
				if (GameController.instance.playerScript.canShoot)
				{
					TPSCamera.instance.SetStreifCamera();
					GameController.instance.playerScript.isShooting = true;
				}
			}
			else
			{
				GameController.instance.playerScript.isShooting = false;
			}
		}
		else if (PlayerBehavior.MyPlayer.currentCar is TitanBehavior)
		{
			GameController.instance.playerScript.isShooting = false;
			((TitanBehavior)PlayerBehavior.MyPlayer.currentCar).mainWeaponsShooting = shoot3Dtouch;
			((TitanBehavior)PlayerBehavior.MyPlayer.currentCar).additionalWeaponsShooting = shoot3Dtouch;
		}
		else
		{
			if (!PlayerBehavior.MyPlayer.currentCar.carWithWeapon)
			{
				return;
			}
			if (shoot3Dtouch)
			{
				if (!PlayerBehavior.MyPlayer.currentCar.isShooting)
				{
					PlayerBehavior.MyPlayer.currentCar.startShoot();
				}
			}
			else if (PlayerBehavior.MyPlayer.currentCar.isShooting)
			{
				PlayerBehavior.MyPlayer.currentCar.cancelShoot();
			}
		}
	}

	private void Update()
	{
		if (pointerDown)
		{
			Touch? touch = null;
			if (isMobile)
			{
				Touch[] touches = Input.touches;
				for (int i = 0; i < touches.Length; i++)
				{
					Touch value = touches[i];
					if (value.fingerId == fingerId && value.phase != TouchPhase.Ended && value.phase != TouchPhase.Canceled)
					{
						touch = value;
						currentPointerPosition = value.position;
						break;
					}
				}
				if (!touch.HasValue)
				{
					currentPointerPosition = Vector2.zero;
					mouseDelta = Vector2.zero;
					compensationDelta = Vector2.zero;
				}
				if (!touch.HasValue && UICamera.touchPressureSupported && !settings.shoot3DtouchDisabled)
				{
					shoot3Dtouch = touch.Value.pressure / touch.Value.maximumPossiblePressure >= settings.Sensivity3DTouch;
				}
				else
				{
					shoot3Dtouch = false;
				}
			}
			else
			{
				shoot3Dtouch = false;
				currentPointerPosition = Input.mousePosition;
			}
			if (prevTouchPos != Vector2.zero && (touch.HasValue || !isMobile))
			{
				if (currentPointerPosition == prevTouchPos)
				{
					if (stationaryCount < 3)
					{
						stationaryCount++;
						mouseDelta *= 0.5f;
						compensationDelta += mouseDelta;
					}
					else
					{
						mouseDelta = Vector2.zero;
						compensationDelta = Vector2.zero;
					}
				}
				else
				{
					stationaryCount = 0;
					mouseDelta = currentPointerPosition - prevTouchPos - compensationDelta;
					compensationDelta = Vector2.zero;
				}
			}
			else
			{
				mouseDelta = Vector2.zero;
				compensationDelta = Vector2.zero;
			}
			prevTouchPos = currentPointerPosition;
		}
		else
		{
			shoot3Dtouch = false;
		}
		Shoot3Dtouch();
		if (TPSCamera.instance != null)
		{
			TPSCamera.instance.SetMouseDelta(mouseDelta);
		}
		if (FPSCamera.instance != null)
		{
			FPSCamera.instance.SetMouseDelta(mouseDelta);
		}
		currentPointerPosition = Vector2.zero;
	}

	public void SetFingerId()
	{
		Touch[] touches = Input.touches;
		for (int i = 0; i < touches.Length; i++)
		{
			Touch touch = touches[i];
			if (touch.phase == TouchPhase.Began)
			{
				fingerId = touch.fingerId;
				break;
			}
		}
	}

	public void ReturnFingerId()
	{
		fingerId = -1;
	}
}
